CREATE
OR REPLACE PROCEDURE zcrk_auto_to_zcckhc () AS
v_date_str   VARCHAR2(8);  -- 日期部分 YYYYMMDD
    v_seq        NUMBER(4);    -- 流水号
    p_bill_no VARCHAR2(50);
    insert_phid NUMBER;
 BEGIN
    -- 获取当天日期字符串
    v_date_str := TO_CHAR(SYSDATE, 'YYYYMMDD');

    -- 查询当天的最大流水号
    SELECT NVL(MAX(TO_NUMBER(SUBSTR(bill_no, 9, 4))), 0) + 1
      INTO v_seq
      FROM p_form_zcckhcd_m
     WHERE SUBSTR(bill_no, 1, 8) = v_date_str;

    -- 拼接编码 = 日期 + 流水号
    p_bill_no := v_date_str || LPAD(v_seq, 4, '0');

-- 直接 FOR 循环查询结果
    FOR rec IN (
        SELECT
            *
        FROM
            kc_billhead t
        WHERE
            t.user_cllx = '01' and phid_transno = '6' and NOT EXISTS (
            SELECT 1
            FROM kc_billhead t2
            WHERE t2.bill_no = t.bill_no and phid_transno = '6'
            GROUP BY t2.bill_no
            HAVING COUNT(*) > 1
        )
    ) LOOP
    -- 判断条件
    IF rec.tempflg = 2 THEN
    insert_phid :=seq_auto_phid_zcckhc.nextval;
    -- TODO: 在这里写逻辑
        INSERT INTO
            p_form_zcckhcd_m (
                phid,
                code,
                creator,
                editor,
                ng_insert_dt,
                ng_update_dt,
                ng_record_ver,
                cur_orgid,
                ng_phid_org,
                ng_phid_cu, --10
                ng_phid_bp,
                ng_phid_original,
                ng_orgid_original,
                ng_phid_ui_scheme,
                ng_sv_search_key,
                ng_sd_search_key,
                ng_share_sign,
                phid_schemeid,
                imp_info,
                ng_write_source, --20
                ng_data_status,
                arc_flag,
                asr_flag,
                printtime,
                printcount,
                phid_app,
                app_dt,
                app_status,
                wf_flag,
                phid_fill_psn, --30
                phid_org,
                phid_pc,
                bill_dt,
                bill_name,
                bill_no,
                u_cklx,
                u_fbht,
                u_fbd,
                u_ckrq,
                u_bhsjexx, --40
                u_lldw,
                u_bhsjedx,
                u_ck
            )
        VALUES
            (
                insert_phid,
                null,
                rec.creator,
                rec.creator,
                SYSDATE,
                SYSDATE,
                1,
                1,
                1,
                1, --10
                0,
                0,
                0,
                0,
                '10',
                '1',
                5,
                null,
                null,
                0, --20
                0,
                0,
                0,
                null,
                0,
                null,
                null,
                0,
                0,
                rec.creator, --30
                rec.phid_org,
                rec.phid_pc,
                SYSDATE,
                null,
                p_bill_no,
                rec.user_cklx,
                rec.user_fbht,
                rec.user_fbd,
                rec.user_ckrq,
                0 - COALESCE(rec.user_bhsjexx, 0), --40
                rec.phid_classno,
                DECODE(rec.user_bhsjey, NULL, '零', '负' || rec.user_bhsjey), -- -4 不含税金额（元）大写,
                rec.phid_warehouse
            );
        END IF;
        -- 处理明细
        FOR rec_d IN (SELECT * FROM kc_billbody WHERE pphid = rec.phid) LOOP
            INSERT INTO
                p_form_zcckhcd_d1 (
                    phid,
                    code,
                    rel_key1,
                    asr_flag,
                    imp_info,
                    creator,
                    editor,
                    ng_insert_dt,
                    ng_update_dt,
                    ng_record_ver, --10
                    cur_orgid, --11
                    ng_phid_org,
                    ng_phid_cu,
                    ng_phid_bp,
                    ng_phid_original,
                    ng_orgid_original,
                    ng_phid_ui_scheme,
                    ng_sv_search_key,
                    ng_sd_search_key,
                    ng_share_sign, --20
                    ng_write_source,
                    ng_data_status,
                    lineid,
                    pphid,
                    phid_pc,
                    itemid,
                    res_propertys,
                    res_masterdata,
                    phid_resbs,
                    u_kcsl, --30
                    u_cksl,
                    u_hsdj,
                    u_hshj,
                    u_sl,
                    u_se,
                    u_bhsdj,
                    u_bhshj
                )
            VALUES
                (
                    seq_auto_phid_zcckhc_detail.nextval,
                    null,
                    null,
                    0,
                    null,
                    rec.creator,
                    rec.creator,
                    SYSDATE,
                    SYSDATE,
                    1, --10
                    1,
                    1,
                    1,
                    0,
                    0,
                    0,
                    0,
                    '10',
                    '1',
                    5, --20
                    0,
                    0,
                    null,
                    insert_phid,
                    null,
                    rec_d.phid_itemid,
                    null,
                    null,
                    null,
                    rec_d.user_kcsl,--30
                    0 - COALESCE(rec_d.qty,0), 
                    rec_d.taxprc,
                    0 - COALESCE(rec_d.tax_mony,0),
                    rec_d.taxrate,
                    rec_d.tax,
                    rec_d.prc,
                    0 - COALESCE(rec_d.mony, 0)
                );
        END LOOP;

        v_seq := v_seq + 1;
        p_bill_no := v_date_str || LPAD(v_seq, 4, '0');


    END LOOP;

commit;

EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('插入失败，错误信息：' || SQLERRM);

RAISE;

ROLLBACK;

END zcrk_auto_to_zcckhc;